package com.shsoft.myThread;

import com.shsoft.colorfulcc.MainActivity;
import com.shsoft.common.Constant;

import java.util.HashMap;

/**
 * 纯色-跑马灯 子线程
 */
public class SingleHorseThread extends Thread{
    private MainActivity activity;
    private int currentColorNum;  // 记录当前已经显示的预设颜色的编号
    private int bright; // 记录亮度
    private int index; // 记录跑马次数
    private int direction; // 方向
    private int speed; // 移动速度
    private int count; // 灯珠个数
    private int circle; // 单色圈数


    public SingleHorseThread(MainActivity activity) {
        this.activity = activity;
        currentColorNum = 0;
        index = 0;
        bright = Integer.parseInt(activity.horse_bright.getText().toString());
        if(bright * 2 > 255){
            bright = 255;
        }else {
            bright = bright * 2;
        }
        direction = activity.horse_direction_value;
        speed = Integer.parseInt(activity.horse_speed.getText().toString()) * 10;
        count = Integer.parseInt(activity.horse_count.getText().toString());
        circle = Integer.parseInt(activity.horse_circle.getText().toString());
    }

    @Override
    public void run() {
        System.out.println("纯色跑马灯子线程运行");
        while (activity.isPlaying) {
            for (int i = 0; i < activity.colors.size(); i++) { // 根据预设颜色列表来循环多少次
                // 获取当前颜色
                int[] rgb = activity.colors.get(i);
                if (count <= 20) { // 普通模式
                    if (activity.isPlaying) {
                        // 因为只写入count个灯珠的数据，防止其他灯珠显示乱七八糟的的颜色，所以初始化 lights_4display
                        for (int j = 0; j < Constant.LIGHT_COUNT; j++) {
                            if (j < count) {
                                activity.lights_4display.put(j, rgb);
                            } else {
                                activity.lights_4display.put(j, new int[]{1, 1, 1});
                            }
                        }
                    }
                    for (int j = 0; j < circle * 20; j++) {
                        if (activity.isPlaying) {
                            // 每次把所有的颜色按顺时针或者逆时针变换
                            if (direction == Constant.CLOCKWISE) { // 顺时针
                                int[] rgb_tmp = activity.lights_4display.get(Constant.LIGHT_COUNT - 1);
                                for (int m = (Constant.LIGHT_COUNT - 1); m > 0; m--) {
                                    activity.lights_4display.put(m, activity.lights_4display.get(m - 1));
                                }
                                activity.lights_4display.put(0, rgb_tmp);
                            } else { // 逆时针
                                int[] rgb_tmp = activity.lights_4display.get(0);
                                for (int m = 0; m < (Constant.LIGHT_COUNT - 1); m++) {
                                    activity.lights_4display.put(m, activity.lights_4display.get(m + 1));
                                }
                                activity.lights_4display.put((Constant.LIGHT_COUNT - 1), rgb_tmp);
                            }
                            // 让主线程刷新灯珠区域
                            activity.alpha = bright;
                            activity.refreshLightsDiv();
                            try {
                                sleep(speed);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                                break;
                            }
                        }
                    }
                } else { // 叠加模式，比如count = 25，就是从1-5个灯珠轮流跑马
                    for (int c = 0; c < circle; c++) {  // 根据圈数再循环
                        for (int j = 1; j <= count - 20; j++) { // 每一圈再从1-(count - 20)开始循环,j就是这个小循环中灯珠的数量
                            if (activity.isPlaying) {
                                // 因为只写入count个灯珠的数据，防止其他灯珠显示乱七八糟的的颜色，所以初始化 lights_4display
                                for (int m = 0; m < Constant.LIGHT_COUNT; m++) {
                                    if (m < j) {
                                        activity.lights_4display.put(m, rgb);
                                    } else {
                                        activity.lights_4display.put(m, new int[]{1, 1, 1});
                                    }
                                }
                                for (int k = 0; k < 20; k++) {
                                    if (activity.isPlaying) {
                                        // 每次把所有的颜色按顺时针或者逆时针变换
                                        if (direction == Constant.CLOCKWISE) { // 顺时针
                                            int[] rgb_tmp = activity.lights_4display.get(Constant.LIGHT_COUNT - 1);
                                            for (int m = (Constant.LIGHT_COUNT - 1); m > 0; m--) {
                                                activity.lights_4display.put(m, activity.lights_4display.get(m - 1));
                                            }
                                            activity.lights_4display.put(0, rgb_tmp);
                                        } else { // 逆时针
                                            int[] rgb_tmp = activity.lights_4display.get(0);
                                            for (int m = 0; m < (Constant.LIGHT_COUNT - 1); m++) {
                                                activity.lights_4display.put(m, activity.lights_4display.get(m + 1));
                                            }
                                            activity.lights_4display.put((Constant.LIGHT_COUNT - 1), rgb_tmp);
                                        }
                                        // 让主线程刷新灯珠区域
                                        activity.alpha = bright;
                                        activity.refreshLightsDiv();
                                        try {
                                            sleep(speed);
                                        } catch (InterruptedException e) {
                                            e.printStackTrace();
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
